package com.hqyj.controller;

import com.hqyj.dao.classifyDao;
import com.hqyj.pojo.Type;
import com.hqyj.pojo.Type2;
import com.hqyj.service.classifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/classify")
public class ClassifyContrioller {
    @Autowired
    private classifyDao classifyDao;

    @Autowired
    private classifyService classifyService;

    @RequestMapping("/home")
    public ModelAndView ClassifyOne(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");

        ModelAndView mav = new ModelAndView();
        mav.setViewName("Classify");

        ArrayList<Type>  TypeList = new ArrayList<>();


        List listType = classifyService.ClassifyType();
        for (int i = 0; i < listType.size(); i++) {
            List list = classifyService.ClassifymodelBytype((String) listType.get(i));

            ArrayList<Type2> Type2List = new ArrayList<>();
            for (Object one:list){
                List<String> brandList = classifyService.ClassifyBrandbymodel((String) one);

                Type2List.add(new Type2((String) one,brandList));
            }
            TypeList.add(new Type((String) listType.get(i),Type2List));
        }
        System.out.println(TypeList);
        mav.addObject("Type", TypeList);

        return mav;
    }

    @RequestMapping("/addType")
    public void updateClassify(HttpServletRequest req ,HttpServletResponse resp){
        String type = req.getParameter("Type");
        System.out.println(type);
        String model = req.getParameter("Model");
        System.out.println(model);
        String brand = req.getParameter("brand");
        System.out.println(brand);

        int num = classifyDao.updateClassify(brand, type, model);
        try {
            PrintWriter out = resp.getWriter();

            if(num>0) {
                out.print("ok");

            }else out.print("fails");
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/deleteType")
    public void deleteClassify(HttpServletRequest req ,HttpServletResponse resp){
        String brand = req.getParameter("brand");

        int i = classifyDao.forbidenClassify(brand);
        try {
            PrintWriter out = resp.getWriter();
            if(i >0){
                out.print("ok");
            }else out.print("fail");
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
